//
//  SoonAnswerViewController.m
//  Uhomebk
//
//  Created by 揭婧雯 on 16/12/1.
//  Copyright © 2016年 com.segi. All rights reserved.
//

#import "SoonAnswerViewController.h"

@interface SoonAnswerViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIView *boxView;
 
}

@property(nonatomic,strong) NSMutableArray *dataArray;//快捷回复语句数据源数组
@property(nonatomic,strong) UITableView *tableView;
@end

@implementation SoonAnswerViewController

-(void)loadView
{
    CGSize size = [[UIScreen mainScreen] bounds].size;
    UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    self.view = mainView;
    //蒙层
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    [self.view addSubview:bgView];
    
    //快捷回复显示
    boxView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds)-220, CGRectGetWidth(self.view.bounds), 220)];
    boxView.backgroundColor = kColor_W;
    [self.view addSubview:boxView];
    
    //关闭快捷语句界面
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeView)];
    [bgView addGestureRecognizer:tap];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(boxView.bounds), CGRectGetHeight(boxView.bounds)) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    [boxView addSubview:_tableView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    NSString *defaultStr = @"[\"已完成\", \"请立即处理\",\"重要，请重点关注\",\"待料，申请挂起\",\"已和业主沟通，延期\",\"派单错误，申请撤回\",\"暂时没空，无法及时处理\",\"非本部门处理，请重新派单\",\"正在忙暂时无法处理，需要延期\"]";
    NSArray *defaultArray = (NSArray *)[SEGUtils dictionaryWithJsonString:defaultStr];
    
    //加载快捷回复模板数据
    if (self.value && ![self.value isEqualToString:@""]) {
        NSArray *dataArr = nil;
        if ([_typeStr isEqualToString:@"evaluate_reply"]) {//评价及原因控件原因数组在message里面
            NSDictionary *dic = (NSDictionary *)[SEGUtils dictionaryWithJsonString:self.value];
            dataArr = dic[@"message"];
        } else {
            dataArr = (NSArray *)[SEGUtils dictionaryWithJsonString:self.value];
        }
        if (dataArr.count > 0) {
            [self.dataArray addObjectsFromArray:dataArr];
        } else {
            [self.dataArray addObjectsFromArray:defaultArray];
        }
    } else {
       [self.dataArray addObjectsFromArray:defaultArray];
    }
    [_tableView reloadData];
}

//点击蒙层关闭选择界面
-(void)closeView
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}


#pragma mark -- tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (cell==nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, CGRectGetWidth(self.view.bounds), 44)];

        [titleLabel setFont:kFont_16];
        [titleLabel setTextColor:kColor_1];
        titleLabel.tag = 101;
        [cell.contentView addSubview:titleLabel];
    }
    UILabel *titleLabel = [cell.contentView viewWithTag:101];
    titleLabel.text = _dataArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self closeView];
//   SoonAnswerEntity *entity = [_dataArray objectAtIndex:indexPath.row];
    NSString *answerStr = _dataArray[indexPath.row];
    //选择快速回复语句回调
    if (_zk_delegate) {
        [_zk_delegate SelectSoonAnswerFinished:answerStr];
    }
    
}

#pragma mark -- 懒加载
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray  = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

@end
